﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;
namespace IHISCETeim.BLL
{
    /// <summary>
    /// 保修卡，调查表
    /// </summary>
    public class UWarrantyCardController : BaseController
    {

        #region 保修卡部份
        /// <summary>
        /// 添加何修卡
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(UWarrantyCardInfo obj)
        {
            SyncSAPController.SyncAgentList();
            int r = UWarrantyCardDAL.Add(obj);
            if (r <= 0)
                return r;
            UMacKeepInfo UKObj = new UMacKeepInfo();
            UKObj.SerialNumber = obj.SerialNumber.ToUpper().Trim();
            UKObj.IsWarrantyCard = true;
            UKObj.IntoDate= obj.IntoDate;
            UKObj.IntoHours= obj.IntoHours;
            UKObj.CustomerName = obj.CustomerName;
            UKObj.CustomerMobile = obj.CustomerMobile;
            UKObj.CustomerAddress = obj.CustomerAddress;
            UKObj.AgentName = obj.AgentName;
            UKObj.AgentTel = obj.AgentTel;
            UKObj.Remark= "";
            UKObj.FirstCostDate="";
            UKObj.FirstCost=0m;
            UKObj.SecondCostDate="";
            UKObj.SecondCost= 0m;
            UKObj.CostRemark = "";


            if (OUserController.CurrentUser.UserType == 2)
            {
                UKObj.SrvActRealName = OUserController.GetById(OUserController.CurrentUser.SrvActUserId).RealName;
            }


            UMacKeepController.Add(UKObj);
            return r;
        }
        public static void AddUQuestionnaire(List<UQuestionnaireInfo> obj)
        {
            UWarrantyCardDAL.AddUQuestionnaire(obj);
        }
        public static bool Delete(int UWCID)
        {
            return UWarrantyCardDAL.Delete(UWCID);
        }
        /// <summary>
        /// 根据ID获取保修卡信息
        /// </summary>
        /// <param name="UWCID"></param>
        /// <returns></returns>
        public static UWarrantyCardInfo GetByUWCID(int UWCID)
        {
            UWarrantyCardInfo o= CBO.FillObject<UWarrantyCardInfo>(UWarrantyCardDAL.GetByUWCID(UWCID));
            if (o == null)
                o = new UWarrantyCardInfo();
            return o;
        }
        //string MachineType, string SerialNumber, string CustomerName, string CustomerIDCard, string AgentName, string Birthday, string AgentAddress, int IsVip, string IntoDateFrom, string IntoDateTo,
        public static DataTableCollection GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return UWarrantyCardDAL.GetPagination(WhereSql, PageIndex, PageSize, Sort, ref  TotalCount);
        }
        /// <summary>
        /// 修改保修卡
        /// </summary>
        /// <returns></returns>
        public static bool Update(UWarrantyCardInfo newobj,UWarrantyCardInfo oldobj)
        {
            SyncSAPController.SyncAgentList();

            bool x = UWarrantyCardDAL.Update(newobj);
            if (!x)
                return x;
            UMacKeepController.Update(newobj,oldobj);
            return x;
        }

        public static UWarrantyCardInfo GetBySerialNumber(string SerialNumber)
        {
            UWarrantyCardInfo obj=CBO.FillObject<UWarrantyCardInfo>(UWarrantyCardDAL.GetBySerialNumber(SerialNumber));
            if(obj==null)
                obj=new UWarrantyCardInfo();
            return obj;
        }
        /// <summary>
        /// 获取代理商机器数量
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAgentMacNum()
        {
            return UWarrantyCardDAL.GetAgentMacNum();
        }
        #endregion

        #region 调查表
        
        /// <summary>
        /// 获取调查表所有项
        /// </summary>
        /// <param name="UWCID"></param>
        /// <returns></returns>
        public static List<UQuestionnaireInfo> GetUQuestionnaireByUWCID(int UWCID)
        {
            return CBO.FillCollection<UQuestionnaireInfo>( UWarrantyCardDAL.GetUQuestionnaireByUWCID(UWCID));
        }

        #endregion

        #region 调查表模板
        /// <summary>
        /// 根据上级ID获取所有的调查表模板数据
        /// </summary>
        /// <param name="ParId"></param>
        /// <returns></returns>
        public static List<UQuestionTempleteInfo> GetAllUQuestionTempleteByParId(int ParId)
        {
            return CBO.FillCollection<UQuestionTempleteInfo>(UWarrantyCardDAL.GetAllUQuestionTempleteByParId(ParId));
        }
        #endregion

        public static DataTable GetReport(int ParId, string fromdate, string todate)
        {
            return UWarrantyCardDAL.GetReport(ParId, fromdate, todate);
        }

        public static int GetReprotSUMCount(string fromdate, string todate)
        {
            return UWarrantyCardDAL.GetReprotSUMCount(fromdate, todate);
        }
        #region 报表
        /// <summary>
        /// 根据时间段统计各月交机第一次出现故障的平均值
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection GetFirstBugTimeReport(string fromdate, string todate)
        {
            return UWarrantyCardDAL.GetFirstBugTimeReport(fromdate, todate);
        }
        #endregion
    }
}
